import requests
import random
import ddddocr
from PIL import Image
import io
import base64

ocr = ddddocr.DdddOcr(show_ad=False, det=False, ocr=False)

num = random.random()
session = requests.Session()
res = session.get(f'https://bm.ruankao.org.cn/sign/captchaImg?{num}')
print(session.cookies)
with open('./static/slide/ruankao.jpg', 'wb') as f:
    f.write(res.content)

img = Image.open('./static/slide/ruankao.jpg')

background_img = img.crop((0, 0, 240, 150))
background_bytes = io.BytesIO()
background_img.save(background_bytes, format='PNG')

target_img = img.crop((0, 151, 50, 300))
target_bytes = io.BytesIO()
target_img.save(target_bytes, format='PNG')

res = ocr.slide_match(target_bytes.getvalue(), background_bytes.getvalue())
target = res['target']
print(target)

res = session.get(f'https://bm.ruankao.org.cn/sign/checkTnCode?p={target}')
print(session.cookies)
# if '成功' in res.json()['msg']:
#     res = session.post('https://bm.ruankao.org.cn/sign/inok', data={
#         's1': 'MTExMTExMTExMTExMTExMTEx',
#         's2': "e10adc3949ba59abbe56e057f20f883e",
#         's3': target,
#     })
#     print(res.json())
# else:
#     print('验证码错误')
